/**
 *
 */
package messaage;

import forum.Forum;


import java.util.ArrayList;
import java.util.List;

/**
 * @author navea
 * 
 */

public class GotListOfForumsMsg extends Message {

	private String strOfForums;
	private List<Forum> listOfforums;

	
	public GotListOfForumsMsg(String lof) {
		this.strOfForums = lof;
		this.listOfforums = new ArrayList<Forum>();
		if(lof.equals("\0")) {
			strOfForums = "ForumsListReply\n\0";
		} else {
			this.strOfForums = "ForumsListReply" + this.strOfForums;
			this.parse();
		}
	}
	
	public GotListOfForumsMsg(String username, List<Forum> forums) {
		this.username = username;
		this.listOfforums = forums;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see messaage.Message#toString()
	 */
	@Override
	public String toString() {
		String ans = "";
		for(int i=0; i < this.listOfforums.size(); i++) {
			ans += "ForumNode:\n";
			ans += Integer.toString(this.listOfforums.get(i).getID()) + "\n";
			ans += this.listOfforums.get(i).getSubject() + "\n";
		}
		return "ForumsListReply\n" + ans + "\0";
	}

	public String getStrOfForums() {
		return this.strOfForums;
	}

	public List<Forum> getListOfForums() {
		return this.listOfforums;
	}

	private void parse() {
		String data = this.strOfForums
				.substring("ForumsListReply\n".length() - 1);
		String[] splitted = data.split("\n");
		int size = splitted.length;
		if (splitted[size - 1].equals("\0"))
			size--;
		for (int i = 1; i < size - 2 ; i = i + 3) {
			Forum f = new Forum(Integer.parseInt(splitted[i + 1]),
					splitted[i + 2]);
			this.listOfforums.add(f);
		}
	}

}
